package com.chenyue.cm.user.service.impl;


import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.constant.Constants;
import com.chenyue.cm.common.utils.StringUtils;
import com.chenyue.cm.user.client.MallGoodsServiceClient;
import com.chenyue.cm.user.dao.JdOrderInfoMapper;
import com.chenyue.cm.user.domain.JdOrderInfo;
import com.chenyue.cm.user.domain.vo.OrderItemDetailVo;
import org.springframework.beans.factory.annotation.Qualifier;
import com.chenyue.cm.user.dao.JdOrderItemMapper;
import com.chenyue.cm.user.domain.JdOrderItem;
import com.chenyue.cm.user.service.JdOrderItemService;
import org.springframework.stereotype.Service;


/**
*
* @author chenyue-cm
*/
@Service(value = "JdOrderItemService")
public class JdOrderItemServiceImpl extends BaseServiceImpl<JdOrderItem>  implements JdOrderItemService {

	private final JdOrderItemMapper jdOrderItemMapper;
	@Resource
	private JdOrderInfoMapper jdOrderInfoMapper;
	@Resource
	private MallGoodsServiceClient goodsServiceClient;

	public JdOrderItemServiceImpl(@Qualifier("jdOrderItemMapper") JdOrderItemMapper jdOrderItemMapper) {
		this.jdOrderItemMapper = jdOrderItemMapper;
	}

	@Override
	@Resource(name = "jdOrderItemMapper")
	public void setBaseMapper(LcBaseMapper<JdOrderItem, Serializable> baseMapper) {
		this.baseMapper = baseMapper;
	}

	@Override
	public List<JdOrderItem> getOrderItemListByOrderId(String orderId, String skuId, String keyWords, String flag) {
		LambdaQueryWrapper<JdOrderItem> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(JdOrderItem::getOrderId, orderId);
		if (skuId != null) {
			wrapper.eq(JdOrderItem::getSkuId, skuId);
		}
		if (StringUtils.isNotEmpty(keyWords)) {
			wrapper.like(JdOrderItem::getGoodsName, keyWords);
		}
		if(StringUtils.isNotEmpty(flag)&&flag.equals("3")){
			wrapper.ne(JdOrderItem::getOrderItemStatus, Constants.ORDER_STATUS_2);
		}
		List<JdOrderItem> orderItemList = jdOrderItemMapper.selectList(wrapper);
		if (orderId.equals(keyWords)) {
			LambdaQueryWrapper<JdOrderItem> w = new LambdaQueryWrapper<>();
			w.eq(JdOrderItem::getOrderId, orderId);
			orderItemList = jdOrderItemMapper.selectList(w);
		}
		return orderItemList;
	}



	@Override
	public List<OrderItemDetailVo> getOrderItemList(String id, String o, String keyWords, String o1) {
		List<OrderItemDetailVo> result = new ArrayList<>();
		List<JdOrderItem> orderItemList = getOrderItemListByOrderId(id, o, keyWords,o1);
		String imgUrl = null;
		for (JdOrderItem orderItem : orderItemList) {
			//【feign远程调用】--获取SPU详情
			Success e = goodsServiceClient.getSpuDetailById(orderItem.getGoodsId());
			Map<String, Object> spuDetailMap = JSON.parseObject(JSON.toJSONString(e.getResult()));
			imgUrl = spuDetailMap.get("imgUrl").toString();
			OrderItemDetailVo vo = new OrderItemDetailVo();
			vo.setGoodId(orderItem.getGoodsId()); //商品id
			vo.setSkuNum(orderItem.getSkuNum().toString());  //数量
			//规格id
			vo.setSkuId(orderItem.getSkuId());
			vo.setGoodName(orderItem.getGoodsName()); //商品名称
			vo.setGoodImg(imgUrl);   //商品图片
			vo.setTotalPrice(orderItem.getTotalPrice().divide(new BigDecimal(orderItem.getSkuNum()), 2, BigDecimal.ROUND_HALF_UP).toString());   //原价
			vo.setFeeMoney(orderItem.getFeeMoney() == null ? "0" : orderItem.getFeeMoney().toString());  //优惠金额
			vo.setPayMoney(orderItem.getPaymentMoney().toString());  //实付金额
			vo.setAttrInfo(orderItem.getSkuDetail());  //规格信息
			vo.setStatus(orderItem.getOrderItemStatus());
			result.add(vo);
		}
		return result;
	}
}
